package com.lemon.ai.socket.packets.spiritgame.xls2Object.generator;

import java.io.File;
import java.io.FileInputStream;

import com.lemon.ai.socket.packets.spiritgame.model.generator.AbstractGenerator;
import com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils;
import com.lemon.ai.socket.packets.spiritgame.model.utils.FileUtils;
import com.lemon.ai.socket.packets.spiritgame.xls2Object.Constent;

public class JavaClassGenerator extends AbstractGenerator {

	public JavaClassGenerator() {
		super("baseClassTemp.txt");

		try {
			this.prop.load(new FileInputStream(new File(System
					.getProperty("user.dir")
					+ "\\templetConfig.properties")));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void generate(Constent c) {
		String temp = "//" + c.explain + "\r" ;

		String implement = "";

		String[] impl = super.prop.getProperty("implements").split(";");
		for (String im : impl) {
			String[] classes  = im.split(":") [1].split(",");

			for (String className : classes) {
				if(className.equals(c.className)) {
					if(implement.isEmpty()) {
						implement =	 " implements " + im.split(":")[0];
					} else {
						implement += "," + im.split(":")[0];
					}
				}
			}
		}
		
		String extendss = "";
		
		String[] ext = super.prop.getProperty("extends").split(";");
		for (String et : ext) {
			String[] classes  = et.split(":") [1].split(",");

			for (String className : classes) {
				if(className.equals(c.className)) {
					if(extendss.isEmpty()) {
						extendss =	 " extends " + et.split(":")[0];
					} else {
						extendss += "," + et.split(":")[0];
					}
				}
			}
		}
		
		temp += super.temp.
				replace("PACKAGE_NAME", super.prop.getProperty("package2")).
				replace("CLASS_NAME", c.className + "Templet"  + extendss + implement ).
				replace("PROPERTIES", buildProperties(c)).
				replace("SETS_AND_GETS", buildGetterSetters(c));

		String fileName = super.prop.getProperty("javaClassPath")
				+ super.prop.getProperty("package2").replace(".", "\\")
				+ "\\" + c.className + "Templet"
				+ ".java";

		fu.write(fileName, temp);
	}


	private static FileUtils fu = new FileUtils();



	private String METHOD_TEMP = 
			"	/**\r" +
			"	 * EXPlAIN\r" +
			"	 */\r" +
			"	public void setMETHOD_NAME(TYPE_NAME PROPERTY_NAME) {\r" + 
					"		this.PROPERTY_NAME = PROPERTY_NAME;\r" + 
					"	}\r\r" + 
					"	/**\r" +
					"	 * EXPlAIN\r" +
					"	 */\r" +
					"	public TYPE_NAME getMETHOD_NAME() {\r" + 
					"		return this.PROPERTY_NAME;\r" + 
					"	}\r\r";

	private String buildGetterSetters(Constent c) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < c.filedNames.length; i++) {
			String f = c.filedNames[i];
			String t = c.types[i];
			sb.append(
					METHOD_TEMP.
					replaceAll("EXPlAIN", c.explains[i]).
					replaceAll("METHOD_NAME", StringUtils.firstToUpperCase(f)).
					replaceAll("TYPE_NAME", t).
					replaceAll("PROPERTY_NAME", f)
					);
		}

		return sb.toString();
	}

	private String PROPERTY_TEMP = 
			"	/**\r" +
			" 	 * EXPlAIN\r" +
			" 	 */\r" +
			"	private TYPE_NAME PROPERTY_NAME;\r";

	private String buildProperties(Constent c) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < c.filedNames.length; i++) {
			String f = c.filedNames[i];
			String t = c.types[i];
			sb.append(
					PROPERTY_TEMP.
					replaceAll("EXPlAIN", c.explains[i]).
					replaceAll("TYPE_NAME", t).
					replaceAll("PROPERTY_NAME", f)
					);
		}

		return sb.toString();
	}
}
